/* Copyright 2009
 *
 * This program and the accompanying materials
 * are made available under the terms of the
 * Eclipse Public License v1.0 which accompanies
 * this distribution, and is available at
 *
 * 		http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in
 * writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
 * OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing
 * permissions and limitations under the License.
 *
 * Contributors:
 * 	   IBM Corporation - initial API and implementation for JDT/DLTK
 *     Sean W. Quinn - initial adoption for use with PHP from various sources.
 */
package org.eclipse.php.internal.ui.preferences.formatter.profile;

import java.util.Map;

import org.eclipse.dltk.ui.formatter.IProfile;

/**
 * Represents a user-defined profile. A custom profile can be modified after
 * instantiation.
 *
 * @author Sean W. Quinn (swquinn@gmail.com)
 */
public class CustomProfile extends PHPProfile {
  protected PHPProfileManager fManager;

  /**
   * <p>
   * Instantiates a new instance of the <code>CustomProfile</code>.
   * </p>
   *
   * @param name the name of this <code>Profile</code>.
   * @param settings the settings for this <code>Profile</code>.
   * @param version the current version of this <code>Profile</code>.
   * @param profileKind the kind of this <code>Profile</code>.
   */
  public CustomProfile(final String name,
      final Map<String, String> formatterSettings, final int version,
      final String kind) {
    super(name, formatterSettings, version, kind);
  }

  /**
	 *
	 */
  @Override
  public PHPProfile rename(String name, PHPProfileManager manager) {
    final String trimmed = name.trim();
    if (trimmed.equals(getName()))
      return this;

    String oldID = getID(); // remember old id before changing name
    setName(trimmed);

    manager.profileRenamed(this, oldID);
    return this;
  }

  /**
   * <p>
   * Assigns the <tt>settings</tt> passed to this profile, provided that they
   * are not <tt>null</tt>. If <tt>null</tt> an exception of the type
   * {@link IllegalArgumentException} will bve thrown, as we should not allow
   * <tt>null</tt> settings.
   * </p>
   *
   * @param settings the formatter settings.
   */
  @Override
  public void setSettings(final Map<String, String> settings) {
    if (settings == null) {
      throw new IllegalArgumentException();
    }
    setSettingsInternal(settings);
    if (fManager != null) {
      fManager.profileChanged(this);
    }
  }

  /**
   * @param profileManager
   */
  public void setManager(PHPProfileManager profileManager) {
    fManager = profileManager;
  }

  /**
   * @return
   */
  public PHPProfileManager getManager() {
    return fManager;
  }

  /**
   * <p>
   * Provides access to the profile's version property. This allows a
   * {@link CustomProfile} to reversion itself.
   * </p>
   *
   * @param version the version.
   */
  public void setVersion(int version) {
    setVersionInternal(version);
  }

  /**
   * <p>
   * Compares the passed <code>Profile</code> to <tt>this</tt>. Because a
   * {@link SharedProfile} extends the {@link CustomProfile} we must first check
   * to see whether or not the profile is an instance of a
   * <code>SharedProfile</code> or not. If it is, the two profiles are not equal
   * and we return <tt>-1</tt>.
   * </p>
   * <p>
   * If the two profiles are both of the exact same type (
   * <code>CustomProfile</code>) and have the same name (case-insensitive) then
   * we return <tt>true</tt>.
   * </p>
   *
   * @param profile the profile to be compared.
   * @return a negative integer, zero, or a positive integer as this object is
   *         less than, equal to, or greater than the specified object.
   */
  public int compareTo(final IProfile profile) {
    if (profile instanceof SharedProfile) {
      return -1;
    }
    if (profile instanceof CustomProfile) {
      return getName().compareToIgnoreCase((profile).getName());
    }
    return 1;
  }

  /**
   * @return <tt>true</tt>, this profile is one that is capable of being saved.
   */
  @Override
  public boolean isProfileToSave() {
    return true;
  }
}
